package com.ycw.controller.cjy;

import java.io.File;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ycw.bean.BasicInfo;
import com.ycw.bean.ProDetaile;
import com.ycw.bean.ProInfo;
import com.ycw.bean.Projects;
import com.ycw.service.xiaocai.FileUploadService;
import com.ycw.service.xiaocai.InsertProjectsService;
import com.ycw.util.Msg;

@Controller
public class FileUploadController {
	
	@Autowired
	FileUploadService fileUploadService;

	@Autowired
	//合并到projects
	InsertProjectsService insertProjectsService;
	// 实现项目图片上传
	@RequestMapping(value="/Upload",method=RequestMethod.POST)
    public String upLoad(@RequestParam("file")MultipartFile file,ProDetaile proDetaile,HttpSession session)  {
		System.out.println("文件上传");
		if (!file.isEmpty()) {//判断文件是否为空
			File files=new File("D:/downloads");
			if(!files.exists()){//如果文件夹不存在
				files.mkdir();//创建文件夹
			}
			String fileName = file.getOriginalFilename();//获取文件名字
			try {
				File file2 = new File(files+File.separator+fileName);
	                file.transferTo(file2);//把文件写入目标文件地址
	                proDetaile.setProdetaileVedio(file2.getAbsolutePath());
	                session.setAttribute("proDetaile", proDetaile);
	                System.out.println("----"+proDetaile.getProdetaileVedio());
	                fileUploadService.UploadPhoto(proDetaile);
	                BasicInfo basicInfo=(BasicInfo)session.getAttribute("basicInfo");
	        		ProInfo proInfo=(ProInfo)session.getAttribute("proInfo");
	        		proDetaile=(ProDetaile)session.getAttribute("proDetaile");
	        		Projects projects=new Projects();
	        		projects.setPsUsId(basicInfo.getBasicinfoUsId());
	        		projects.setPsCustName(basicInfo.getBasicinfoCustName());
	        		projects.setPsCustPhone(basicInfo.getBasicinfoCustPhone());
	        		projects.setPsPstId(basicInfo.getBasicinfoPstId());
	        		projects.setPsCustAddress(basicInfo.getBasicinfoCustAddress());
	        		projects.setPsFile(proInfo.getProinfoFile());
	        		projects.setPsName(proInfo.getProinfoName());
	        		projects.setPsGoal(proInfo.getProinfoGoal());
	        		projects.setPsAddress(proInfo.getProinfoAddress());
	        		projects.setPsMoney(proInfo.getProinfoMoney());
	        		projects.setPsDays(proInfo.getProinfoDatenum());
	        		projects.setPsVideo(proDetaile.getProdetaileVedio());
	        		System.out.println("====="+projects.getPsVideo());
	        		projects.setPsStory(proDetaile.getProdetaileStory());
	        		projects.setPsSupport(proDetaile.getProdetaileSupport());
	        		projects.setPsRepay(proDetaile.getProdetaileRepay());
	        		projects.setPsAboutme(proDetaile.getProdetaileAboutme());
	        		insertProjectsService.insertProjects(projects);
	        		return "redirect:/index.jsp";

	            } catch (Exception e) {

	                e.printStackTrace();

	                return "redirect:/index.jsp";

	            }

		}
		 return "redirect:/index.jsp";//返回到成功页面
	 }
}
	
 